#include <QDebug>
#include <QtTest/QtTest>
#include "grammar/RestoreCodeVisiter.h"
#include "TestParser.h"

ModulePtr TestParser::parse(const QString& str)
{
    CharStream stream(str, str.length(), 0, 0);
    PythonGrammar30TokenManager in(&stream);
    //connect(&err, SIGNAL(error(QString,int,int)), SLOT(parseError(QString,int,int)));
    PythonGrammar30 grammar(&in);

    ModulePtr newRoot = grammar.file_input().dynamicCast<Module>();

    return newRoot;
}

void TestParser::parseError(const QString& msg, int line, int col)
{
    qDebug() << "Line:" << line << "Col:" << col << msg;
}

QString TestParser::restoreCode(const ModulePtr& root)
{
    RestoreCodeVisiter visit;
    root->accept(&visit, root);
    return visit.code();
}

void TestParser::testDel()
{
    QString test = "del x, y, z";
    ModulePtr out = parse(test);
    QVERIFY(test == restoreCode(out));
}

void TestParser::testBinOp()
{
    QString test = "45 !<< 2";
    ModulePtr out = parse(test);
    QVERIFY(test == restoreCode(out));

    test = "13 >> 7";
    out = parse(test);
    QVERIFY(test == restoreCode(out));
}

void TestParser::testForElse()
{
    QString test="\
def f():\n\
    for x in range(10):\n\
        break\n\
    else:\n\
        y = 2\n\
    z = 3\n\
";
    ModulePtr out = parse(test);
    qDebug() << "????" << restoreCode(out);
}

